package org.example.springboot.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.sf.jsqlparser.Model;
import org.apache.catalina.connector.Response;
import org.apache.catalina.filters.ExpiresFilter;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.example.springboot.exception.CustomException;
import org.example.springboot.common.Result;
import org.example.springboot.utils.WechatHttpUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

//标记RestController表明他是对外提供数据接口的类
@RestController
public class WebController {
    private ExpiresFilter.XHttpServletResponse response;

    //通过GetMapping来表明是get请求的接口
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
    @GetMapping("/weather")
    public String weather(){
        return "今天天气很晴朗";
    }
    @GetMapping("/count")
    public Integer count(){
        return 23;
    }
    @GetMapping("/map")
    public Map<String,Object> map() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "邵小瑞");
        map.put("age", 25);
        return map;
    }
    //使用统一包装类返回数据
    //这样返回的类型只用是Result型即可
    //只需要把返回的数据放到data里即可 这样前端可以看到返回成功还是失败 以及只需要关注data里面的数据即可
    @GetMapping
    public Result hello2(){
        return Result.success("hello2");
    }
    @GetMapping("/count2")
    public Result count2(){
        //int a = 1 /0;
        return Result.success(10);//静态方法通过类名调用
    }
    @GetMapping("/weather2")
    public Result weather2(){
        return Result.success("今天天气很晴朗");
    }
    @GetMapping("/map2")
    public Result map2(){
        HashMap<String,Object> map = new HashMap<>();
        map.put("name","邵小瑞");
        map.put("age",25);
        return Result.success(map);
    }

    //全局异常处理
    @GetMapping("/count3")
    public Result count3(){
        //int a = 1 / 0;
        return Result.success(10);
    }
    //自定义异常
    @GetMapping("/count4")
    public Result count4(){
        throw new CustomException("400","错误！禁止请求");
        //throw new RuntimeException("自定义错误");
    }


//    @GetMapping("/wxLogin")
//    public Result wxLogin() throws Exception {
//        //redirectUrl是回调的地址 要转成UrlEncode格式
//        String redirectUrl = URLEncoder.encode("http://126d01fc.r7.cpolar.top/wxCallback","UTF-8");
//        //构造二维码链接地址
//        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4eb6934fdc4dd7b6 " +
//                redirectUrl +
//                "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
//        //生成二维码的，扫描后跳转上面的地址
//        response.setContentType("image/png");
//        QrCodeUtil.generate("http://localhost:8080/video/select",300,300, "jpg",response.getOutputStream());
//        return Result.success();
//    }


//获取code生成一个二维码
    @GetMapping("/wxLogin")
    public Result wxLogin() throws Exception {
        //redirectUrl是回调的地址 要转成UrlEncode格式
        String redirectUrl = URLEncoder.encode("https://1114urmk41092.vicp.fun/wechat/callback", "UTF-8");
        //构造二维码链接地址
        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4eb6934fdc4dd7b6" +
                redirectUrl +
                "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        return Result.success("redirect:" + url);
    }
    //通过code获取acess_tocken
    @GetMapping("callback")
    public String callback(String code, String state, Model model) {
        String baseAccessTokerUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx4eb6934fdc4dd7b6"+
                "&secret=e656a4d2627dc61af489ca9d1b2bd55b"+
                code +
                "&grant_type=authorization_code";
        HttpGet  httpGet = new HttpGet(baseAccessTokerUrl);
        CloseableHttpResponse response = null;
        try {
            response = WechatHttpUtils.getClient().execute(httpGet);
            int status = response.getStatusLine().getStatusCode();
            JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(response.getEntity()));
            String access_token = jsonObject.getString("access_token");
            String openid = jsonObject.getString("openid");
            String unionid = jsonObject.getString("unionid");

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "";
    }
}
